package cn.seecoder.courselearning.serviceimpl.post;

import cn.seecoder.courselearning.dto.post.PostDTO;
import cn.seecoder.courselearning.dto.post.ReplyDTO;
import cn.seecoder.courselearning.mapperservice.post.PostMapper;
import cn.seecoder.courselearning.mapperservice.post.ReplyMapper;
import cn.seecoder.courselearning.po.post.PostPO;
import cn.seecoder.courselearning.po.post.ReplyPO;
import cn.seecoder.courselearning.service.post.PostService;
import cn.seecoder.courselearning.vo.ResultVO;
import cn.seecoder.courselearning.vo.post.PostVO;
import cn.seecoder.courselearning.vo.post.ReplyVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PostServiceImpl implements PostService {
    @Autowired
    public PostMapper postMapper;

    @Autowired
    public ReplyMapper replyMapper;

    @Override
    public ResultVO<PostVO> createPost(PostDTO postDTO) {
        PostPO postPO=new PostPO(postDTO);
        if (postMapper.addPost(postPO)==1) {
            return new ResultVO<>(1,"操作成功");
        } else {
            return new ResultVO<>(0,"操作失败");
        }
    }

    @Override
    public ResultVO deletePost(PostDTO postDTO) {

        if (postMapper.deletePost(postDTO.getPostId())==1){
            return new ResultVO<>(1,"操作成功");
        }else{
            return new ResultVO<>(0,"操作失败");
        }
    }

    @Override
    public PageInfo<PostVO> searchPosts(int courseId, int pageNo,int number) {
        List<PostPO> postPOS=postMapper.selectPostByCourseId(courseId);
        List<PostVO> postVOS=null;
        for (int i=0;i<postPOS.size();i++){
            postVOS.add(new PostVO(postPOS.get(i)));
        }
        PageHelper.startPage(pageNo,number);
        return new PageInfo<PostVO>(postVOS);
    }

    @Override
    public PageInfo<ReplyVO> searchReplies(int postId, int pageNo,int number) {
        List<ReplyPO> replyPOS=replyMapper.selectReplyByPostId(postId);
        List<ReplyVO> replyVOS=null;
        for (int i=0;i<replyPOS.size();i++){
            replyVOS.add(new ReplyVO(replyPOS.get(i)));
        }
        PageHelper.startPage(pageNo,number);
        return new PageInfo<ReplyVO>(replyVOS);
    }

    @Override
    public ResultVO<ReplyVO> createReply(ReplyDTO replyDTO) {
        ReplyPO replyPO=new ReplyPO(replyDTO);
        if (replyMapper.reply(replyPO)==1) {
            return new ResultVO<>(1,"操作成功");
        } else {
            return new ResultVO<>(0,"操作失败");
        }
    }

    @Override
    public ResultVO<ReplyVO> createReply2Reply(ReplyDTO replyDTO) {
        ReplyPO replyPO=new ReplyPO(replyDTO);
        if (replyMapper.replyReply(replyPO)==1) {
            return new ResultVO<>(1,"操作成功");
        } else {
            return new ResultVO<>(0,"操作失败");
        }
    }

    @Override
    public PageInfo<ReplyVO> searchReply2Reply(int replyId, int pageNo,int number) {
        List<ReplyPO> replyPOS=replyMapper.selectReplyByReplyId(replyId);
        List<ReplyVO> replyVOS=null;
        for (int i=0;i<replyPOS.size();i++){
            replyVOS.add(new ReplyVO(replyPOS.get(i)));
        }
        PageHelper.startPage(pageNo,number);
        return new PageInfo<ReplyVO>(replyVOS);
    }

    @Override
    public PageInfo<PostVO> searchUserPosts(int userId,int pageNo,int number) {
        List<PostPO> postPOS=postMapper.selectPostByUserId(userId);
        List<PostVO> postVOS=null;
        for (int i=0;i<postPOS.size();i++){
            postVOS.add(new PostVO(postPOS.get(i)));
        }
        PageHelper.startPage(pageNo,number);
        return new PageInfo<PostVO>(postVOS);
    }

    @Override
    public PageInfo<ReplyVO> searchUserReplies(int userId,int pageNo,int number) {
        List<ReplyPO> replyPOS=replyMapper.selectReplyByUserId(userId);
        List<ReplyVO> replyVOS=null;
        for (int i=0;i<replyPOS.size();i++){
            replyVOS.add(new ReplyVO(replyPOS.get(i)));
        }
        PageHelper.startPage(pageNo,number);
        return new PageInfo<ReplyVO>(replyVOS);
    }
}
